package com.example.huawei.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.huawei.system.entity.CommentRecord;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.CommentRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "评论相关接口")
@RestController
@RequestMapping("/comment")
public class CommentRecordController {

    @Autowired
    private CommentRecordService commentRecordService;


    @ApiOperation(value = "一级评论分页查询", notes = "一级评论分页查询")
    @GetMapping("/pageFirst")
    public Result<IPage<CommentRecord>> queryPageFirst(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                       CommentRecord commentRecord) {
        Page<CommentRecord> page = new Page<>(pageNo, pageSize);
        return Result.ok(commentRecordService.queryFirst(page,commentRecord));
    }

    @ApiOperation(value = "二级评论分页查询", notes = "二级评论分页查询")
    @GetMapping("/pageSecond")
    public Result<IPage<CommentRecord>> queryPageSecond(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(value = "parentId") String parentId) {
        Page<CommentRecord> page = new Page<>(pageNo, pageSize);
        return Result.ok(commentRecordService.querySecond(page, parentId));
    }


    @ApiOperation(value = "新增评论", notes = "新增评论")
    @PostMapping("/add")
    public Result<?> add(@RequestBody CommentRecord commentRecord) {
        commentRecord.setCreateTime(new Date());
        boolean flag = commentRecordService.save(commentRecord);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "删除评论", notes = "删除评论")
    @DeleteMapping("/del/{id}")
    public Result edit(@PathVariable("id") String id) {
        boolean flag = commentRecordService.removeById(id);
        return flag ? Result.ok() : Result.error();
    }


}
